package com.lykj.modules.shop.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lykj.core.mp.base.BaseServiceImpl;
import com.lykj.core.secure.utils.AuthUtil;
import com.lykj.modules.shop.mapper.StoreProductMapper;
import com.lykj.modules.shop.service.ServerAreaService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.lykj.modules.shop.vo.ServerAreaVo;
import com.lykj.modules.shop.entity.ServerArea;
import com.lykj.modules.shop.mapper.ServerAreaMapper;

import java.util.Arrays;
import java.util.List;
import java.util.Collection;

/**
 * 区域服务商Service业务层处理
 *
 * @author ruoyi
 * @date 2022-08-14
 */
@RequiredArgsConstructor
@Service
public class ServerAreaServiceImpl extends BaseServiceImpl<ServerAreaMapper, ServerArea>  implements ServerAreaService {

    private final ServerAreaMapper baseMapper;
    private final StoreProductMapper productMapper;

    /**
     * 查询区域服务商
     *
     * @param id 区域服务商主键
     * @return 区域服务商
     */
    @Override
    public ServerArea queryById(Long id){
        ServerArea serverArea = baseMapper.selectById(id);
        String products = serverArea.getSubmitProducts();
        if(StringUtils.isNotBlank(products)){
            List<String> list = Arrays.asList(products.split(","));
            serverArea.setProductList(productMapper.selectBatchIds(list));
        }
        return serverArea;
    }

    @Override
    public ServerAreaVo queryByUserId(Long userId) {
        ServerArea serverArea = baseMapper.queryByUserId(userId);
        if (serverArea != null) {
            ServerAreaVo serverAreaVo = BeanUtil.copyProperties(serverArea, ServerAreaVo.class);
            String products = serverAreaVo.getSubmitProducts();
            if (StringUtils.isNotBlank(products)) {
                String[] split = products.split(",");
                serverAreaVo.setProductList(productMapper.selectBatchIds(Arrays.asList(split)));
            }
            return serverAreaVo;
        }
        return null;
    }

    @Override
    public IPage<ServerArea> queryPageList(IPage<ServerArea> page, ServerArea bo) {
        return baseMapper.selectPage(page, buildQueryWrapper(bo));
    }

    /**
     * 查询区域服务商列表
     *
     * @param bo 区域服务商
     * @return 区域服务商
     */
    @Override
    public List<ServerArea> queryList(ServerArea bo) {
        LambdaQueryWrapper<ServerArea> lqw = buildQueryWrapper(bo);
        return baseMapper.selectList(lqw);
    }

    private LambdaQueryWrapper<ServerArea> buildQueryWrapper(ServerArea bo) {
        LambdaQueryWrapper<ServerArea> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getUserId() != null, ServerArea::getUserId, bo.getUserId());
        lqw.like(StringUtils.isNotBlank(bo.getAreas()), ServerArea::getAreas, bo.getAreas());
        lqw.like(StringUtils.isNotBlank(bo.getProducts()), ServerArea::getProducts, bo.getProducts());
        lqw.eq(bo.getStatus() != null, ServerArea::getStatus, bo.getStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getApprovalUser()), ServerArea::getApprovalUser, bo.getApprovalUser());
        lqw.eq(bo.getRatio() != null, ServerArea::getRatio, bo.getRatio());
        lqw.eq(bo.getStartTime() != null, ServerArea::getStartTime, bo.getStartTime());
        lqw.eq(bo.getEndTime() != null, ServerArea::getEndTime, bo.getEndTime());
        lqw.like(StringUtils.isNotBlank(bo.getSubmitAreas()), ServerArea::getSubmitAreas, bo.getSubmitAreas());
        lqw.like(StringUtils.isNotBlank(bo.getSubmitProducts()), ServerArea::getSubmitProducts, bo.getSubmitProducts());
        lqw.eq(bo.getIsConfirm() != null, ServerArea::getIsConfirm, bo.getIsConfirm());
        lqw.eq(bo.getDayMoney() != null, ServerArea::getDayMoney, bo.getDayMoney());
        lqw.eq(bo.getMonthMoney() != null, ServerArea::getMonthMoney, bo.getMonthMoney());
        return lqw;
    }

    /**
     * 新增区域服务商
     *
     * @return 结果
     */
    @Override
    public Boolean insertByBo(ServerArea add) {
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        return flag;
    }

    @Override
    public Boolean updateByBo(ServerArea bo) {
        bo.setApprovalUser(AuthUtil.getUserName());
        if (bo.getIsConfirm()!=null&&bo.getIsConfirm()==1) {
            bo.setProducts(bo.getSubmitProducts());
        }
        return baseMapper.updateById(bo) > 0;
    }


    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(ServerArea entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除区域服务商
     *
     * @return 结果
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> id, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(id) > 0;
    }
}
